/*===========================================================================================================================
	Wolf Prop Protection
		By Goluch
			DO NOT UPLOAD TO GARRYSMOD.ORG
			
			
	Section: Trust
===========================================================================================================================*/
WPP.Trust = { };

/*===========================================================================================================================
	Name: WPP.GetTrust
	Purpose: Get Trust between players.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetTrust(Player,Trustee,Index)
	
	if ( !Player ) or ( !Player:IsPlayer() ) then
		return false;
	end
	
	if ( !Trustee ) or ( !Trustee:IsPlayer() ) then
		return false;
	end
	
	if ( !WPP:IsValidAction(Index) ) then
		return false;
	end
	
	if ( !WPP.Trust[ Player:SteamID() ] ) then
		return false;
	end
	
	if ( !WPP.Trust[ Player:SteamID() ][ Index ] ) then
		return false;
	end
	
	if ( WPP.Trust[ Player:SteamID() ][ Index ][ Trustee:SteamID() ] ) then
		return true;
	end
	
	return false;
end

/*===========================================================================================================================
	Name: WPP.GetTrustGlobal
	Purpose: Get players Global Trust.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetTrustGlobal(Player,Index)
	if ( !WPP:IsValidAction(Index) ) then
		return false;
	end
	
	if ( !WPP.Trust[ Player:SteamID() ] ) then
		return false;
	end
	
	if ( !WPP.Trust[ Player:SteamID() ][ Index ] ) then
		return false;
	end
	
	if ( WPP.Trust[ Player:SteamID() ][ Index ][ "global" ] ) then
		return true;
	end
	
	return false;
end

/*===========================================================================================================================
	Name: WPP.SetTrust
	Purpose: Set Trust between players.
	Author: Goluch
===========================================================================================================================*/
function WPP:SetTrust(Player,Trustee,Index,Bool)
	if ( !WPP:IsValidAction(Index) ) then
		return;
	end
	
	if ( Bool != nil) and ( Bool == false ) then
		Bool = nil;
	end
	
	if ( !WPP.Trust[ Player:SteamID() ] ) then
		WPP.Trust[ Player:SteamID() ] = {};
	end
	
	if ( !WPP.Trust[ Player:SteamID() ][ Index ] ) then
		WPP.Trust[ Player:SteamID() ][ Index ] = {};
	end
	
	WPP.Trust[ Player:SteamID() ].Player = Player;
	
	if ( WPP.Trust[ Player:SteamID() ][ Index ][ Trustee:SteamID() ] == Bool ) then
		return;
	end
	
	WPP.Trust[ Player:SteamID() ][ Index ][ Trustee:SteamID() ] = Bool;
	
	if ( SERVER ) then
		umsg.Start("WPP-UpdateTrust",{Player,Trustee});
			umsg.Entity(Player);
			umsg.Entity(Trustee);
			umsg.Short(Index);
			umsg.Bool(Bool || false);
		umsg.End();
	end
	
end

/*===========================================================================================================================
	Usermessage: WPP-UpdateTrust
	Purpose: Used for cleint sync.
	Author: Goluch
===========================================================================================================================*/
usermessage.Hook("WPP-UpdateTrust",function(UM)
	WPP:SetTrust(UM:ReadEntity(), UM:ReadEntity(), UM:ReadShort(), UM:ReadBool());
end)

/*===========================================================================================================================
	Name: WPP.SetTrustGlobal
	Purpose: GSet global Trust of a player.
	Author: Goluch
===========================================================================================================================*/
function WPP:SetTrustGlobal(Player,Index,Bool)
	if ( !WPP:IsValidAction(Index) ) then
		return;
	end
	
	if ( Bool != nil) and ( Bool == false ) then
		Bool = nil;
	end
	
	WPP.Trust[ Player:SteamID() ] = WPP.Trust[ Player:SteamID() ] || {};
	
	WPP.Trust[ Player:SteamID() ][ Index ] = WPP.Trust[ Player:SteamID() ][ Index ] || {};
	
	if ( WPP.Trust[ Player:SteamID() ][ Index ][ "global" ] == Bool ) then
		return;
	end
	
	WPP.Trust[ Player:SteamID() ][ Index ][ "global" ] = Bool;
	
	if ( SERVER ) then
		umsg.Start("WPP-GlobalTrust");
			umsg.Entity(Player);
			umsg.Short(Index);
			umsg.Bool(Bool || false);
		umsg.End();
	end
	
end

/*===========================================================================================================================
	Usermessage: WPP-GlobalTrust
	Purpose: Used for cleint sync.
	Author: Goluch
===========================================================================================================================*/
usermessage.Hook("WPP-GlobalTrust",function(UM)
	WPP:SetTrustGlobal(UM:ReadEntity(), UM:ReadShort(), UM:ReadBool());
end)

/*===========================================================================================================================
	Name: WPP.WipeTrust
	Purpose: Wipes the entire trust levels of a player.
	Author: Goluch
===========================================================================================================================*/
function WPP:WipeTrust(Player)
	WPP.Trust[ Player:SteamID() ] = nil;
	
	if ( SERVER ) then
		umsg.Start("WPP-WipeTrust");
			umsg.Entity(Player);
		umsg.End();
	end
	
end

/*===========================================================================================================================
	Usermessage: WPP-WipeTrust
	Purpose: Used for cleint sync.
	Author: Goluch
===========================================================================================================================*/
usermessage.Hook("WPP-WipeTrust",function(UM)
	WPP:WipeTrust(UM:ReadEntity());
end)

/*===========================================================================================================================
	Name: WPP.TrustSync
	Purpose: Sends a client the trust data they need.
	Author: Goluch
===========================================================================================================================*/
function WPP:TrustSync(Player)
	if ( CLIENT ) then
		return;
	end
	
	for _,Table in pairs(self.Trust) do
		Truster = Table.Player;
		for Index,Trusts in pairs(Table) do
			if ( Trusts["global"] ) then
				umsg.Start("WPP-GlobalTrust",Player);
					umsg.Entity(Truster);
					umsg.Short(Index);
					umsg.Bool(true);
				umsg.End();
			end
			
			if ( table.HasValue(Trusts,Player:SteamID()) ) then
				umsg.Start("WPP-UpdateTrust",Player);
					umsg.Entity(Player);
					umsg.Entity(Truster);
					umsg.Short(Index);
					umsg.Bool(true);
				umsg.End();
			end
			
		end
		
	end
	
end


hook.Add("PlayerDisconnected","WPP-PlayerDisconnected",function(Player)
	if ( SERVER ) then
		if ( GetConVarNumber("wpp_auto_cleanup") > 0 ) then
			
			if ( GetConVarNumber("wpp_auto_admins") >= 2 ) and ( Player:IsSuperAdmin() ) then
				//Do Nothing
			elseif ( GetConVarNumber("wpp_auto_admins") >= 1 ) and ( Player:IsAdmin() ) then
				//Do Nothing
			else
				timer.Create("WPP-CleanUp-" .. Player:SteamID(), GetConVarNumber("wpp_auto_cleanup"), 1, function(Steam)
					WPP:CleanUp(Steam,true);
				end,
				
				Player:SteamID());
			end
			
		end
	
	end
	
	WPP.Trust[Player:SteamID()] = nil;
end)